//
//  OrderModel.m
//  RainbowFM
//
//  Created by chenli on 14/12/29.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "OrderModel.h"
#import "OrderAPI.h"
#import "LoginModel.h"

@implementation OrderModel

+(instancetype)sharedModel
{
    static OrderModel *orderModel = nil;
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        orderModel = [[OrderModel alloc] init];
    });
    
    return orderModel;
}

-(void)createOrderWithProdID:(NSString *)prodId prodPrice:(NSString *)prodPrice_ prodName:(NSString *)prodName prodImg:(NSString *)prodImage prodNum:(NSString *)prodNum buyerName:(NSString *)buyerName_ buyerPhone:(NSString *)phone buyerAddr:(NSString *)address result:(void (^)(BOOL,NSString *))res{
    OrderAPI *api = [[OrderAPI alloc] init];
    [api createOrderWithUserID:[LoginModel sharedModel].userID
                        ProdID:prodId
                     prodPrice:prodPrice_
                      prodName:prodName
                       prodImg:prodImage
                       prodNum:prodNum
                     buyerName:buyerName_
                    buyerPhone:phone
                     buyerAddr:address result:^(NSString *orderId) {
                         if (orderId!=nil) {
                             res(YES,orderId);
                         }else{
                             res(NO,nil);
                         }
                     }];
}

-(void)paymentWithOrderID:(NSString *)orderId payType:(NSString *)type result:(void(^) (BOOL, NSString *, NSString *))res{
    OrderAPI *api = [[OrderAPI alloc] init];
    [api paymentWithUserID:[LoginModel sharedModel].userID
                   orderID:orderId
                   payType:type result:^(NSString *payURL, NSString *msg) {
                       if (payURL != nil) {
                           res(YES,payURL,@"");
                       }
                       else
                       {
                           res(NO,nil,nil);
                       }
                   }];
}

-(void)refundOrderWithOrderID:(NSString *)orderId desc:(NSString *)des result:(void (^)(BOOL, NSString *))res{
    OrderAPI *api = [[OrderAPI alloc] init];
    [api refundOrderWithUserID:[LoginModel sharedModel].userID
                       orderID:orderId
                          desc:des result:^(BOOL success, NSString *msg) {
                                res(success,msg);
                          }];
}


-(void)delayOrderWithOrderID:(NSString *)orderId result:(void (^)(BOOL, NSString *))res{
    OrderAPI *api = [[OrderAPI alloc] init];
    [api
      delayOrderWithUserID:[LoginModel sharedModel].userID
     orderID:orderId
     result:^(BOOL success, NSString *msg) {
         res(success,msg);
     }];
}

-(void)receiptOrderWithOrderID:(NSString *)orderId result:(void (^)(BOOL, NSString *))res{
    OrderAPI *api = [[OrderAPI alloc] init];
    [api receiptOrderWithUserID:[LoginModel sharedModel].userID
                        orderID:orderId
                         result:^(BOOL success, NSString *msg) {
                             res(success,msg);
                         }];
}

-(void)fetchUserOrderListWithStatus:(NSString *)status currentPage:(NSString *)start result:(void (^)(BOOL, NSArray *, NSString *))res{
    OrderAPI *api = [[OrderAPI alloc] init];
    [api fetchUserOrderListWithUserID:[LoginModel sharedModel].userID
                          orderStatus:@""
                               cancel:@""
                           merchantID:@""
                             pageSize:@"1000"
                          currentPage:start
                               result:^(NSArray *array, NSString *msg) {
                                   if (array != nil) {
                                       NSMutableArray *listArray = [NSMutableArray array];
                                       for (NSDictionary *dict in array) {
                                           [listArray addObject:[[OrderListItem alloc] initWithAttributes:dict]];
                                       }
                                       
                                       res(YES,listArray,@"");
                                   }
                                   else
                                   {
                                       res(NO,nil,@"");
                                   }
                                   
                               }];
}


-(void)cancelOrderWithOrderID:(NSString *)orderId result:(void (^)(BOOL, NSString *))res{
    OrderAPI *api = [[OrderAPI alloc] init];
    [api cancelOrderWithUserID:[LoginModel sharedModel].userID
                       orderID:orderId
                        result:^(BOOL success, NSString *msg) {
                            res(success,msg);
                        }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end


@implementation OrderListItem



@end
